<?php
/**
 * 客房送物
 */

namespace App\Http\Controllers;

use App\Http\Validate\DeliveryValidate;
use App\Models\GoodsStock;
use App\Models\GoodsStockHis;
use App\Models\RoomSendHis;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DeliveryController extends BaseController
{

    /**
     * 送物记录
     * @param Request $request
     * @return void
     */
    public function index(Request $request)
    {

    }

    public function outbound(Request $request)
    {
        $params = $request->all();

        list($roomSendHis, $goods_stock) = (new DeliveryValidate())->outbound($params);

        DB::beginTransaction();
        try {
            // 出库
            $result = GoodsStock::goods_use($roomSendHis->num, $goods_stock);
            if (!$result) {
                return $this->fail(402, '网络错误，请稍后重试');
            }
            // 添加送物记录
            $result = GoodsStockHis::log($this->user->id, $params['goods_id'], '领用', $roomSendHis->num);
            if (!$result) {
                DB::rollBack();
                return $this->fail(402, '添加送物记录，请稍后重试');
            }

            // 修改送物记录状态


        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->fail(10002, "操作失败/" . $exception->getMessage());
        }
        DB::commit();
    }

}
